package com.zouqone.smc.common.utils.http;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * HttpClient工具类
 * 
 */
public class HttpClientUtil {

	public final static String Method_POST = "POST";

	/**
	 * 多重表单提交
	 * 
	 * @param form
	 *            表单数据
	 * @return 提交响应
	 */
	public static String submit(MultipartForm form) {
		String responseStr = "";
		CloseableHttpClient client = HttpClients.createDefault();

		try {
			HttpPost httpPost = new HttpPost(form.getAction());
			MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();

			// 追加普通表单字段
			Map<String, String> normalFieldMap = form.getNormalField();
			for (Entry<String, String> entry : normalFieldMap.entrySet()) {
				entityBuilder.addPart(entry.getKey(), new StringBody(entry.getValue(), ContentType.create("text/plain", Consts.UTF_8)));
			}

			// 追加文件字段
			Map<String, File> fileFieldMap = form.getFileField();
			for (Entry<String, File> entry : fileFieldMap.entrySet()) {
				entityBuilder.addPart(entry.getKey(), new FileBody(entry.getValue()));
			}

			// 设置请求实体
			httpPost.setEntity(entityBuilder.build());

			// 发送请求
			CloseableHttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			// 取得响应数据
			HttpEntity resEntity = response.getEntity();
			if (200 == statusCode) {
				if (resEntity != null) {
					responseStr = EntityUtils.toString(resEntity);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return responseStr;
	}

	/**
	 * 多重表单
	 * <p>
	 * 同时支持字符串、文件
	 */
	public class MultipartForm implements Serializable {
		private static final long serialVersionUID = -2138044819190537198L;
		/**
		 * 提交URL
		 */
		private String action = "";
		/**
		 * 提交方式
		 */
		private String method = "POST";
		/**
		 * 普通表单字段(字符串）
		 */
		private Map<String, String> normalField = new LinkedHashMap<String, String>();
		/**
		 * 文件字段
		 */
		private Map<String, File> fileField = new LinkedHashMap<String, File>();

		public String getAction() {
			return action;
		}

		public void setAction(String action) {
			this.action = action;
		}

		public String getMethod() {
			return method;
		}

		public void setMethod(String method) {
			this.method = method;
		}

		public Map<String, String> getNormalField() {
			return normalField;
		}

		public void setNormalField(Map<String, String> normalField) {
			this.normalField = normalField;
		}

		public Map<String, File> getFileField() {
			return fileField;
		}

		public void setFileField(Map<String, File> fileField) {
			this.fileField = fileField;
		}

		public void addFileField(String key, File value) {
			fileField.put(key, value);
		}

		public void addNormalField(String key, String value) {
			normalField.put(key, value);
		}
	}

}